/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.zr.coverflow.slice;

import com.zr.coverflow.ability.ColorViewpageAbility;
import com.zr.coverflow.ability.ListContainerAbility;
import com.zr.coverflow.ability.TwoViewpageability;
import com.zr.coverflow.ability.ViewPageAbility;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.ListContainer;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;


import java.io.IOException;
import java.util.ArrayList;

import me.crosswall.coverflow.demo.ResourceTable;
import me.crosswall.lib.coverflow.SampleItem;
import me.crosswall.lib.coverflow.SampleItemProvider;

/**
 * MainAbilitySlice
 *
 * @since 2021-03-26
 */
public class MainAbilitySlice extends AbilitySlice {
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(HiLog.LOG_APP, 0, "MainAbilitySlice");
    private ListContainer mainlistcontainer;
    private String[] stringdata;
    private final String connect = "a linkage pager demo";
    private final int mSum1 = 1;
    private final int mSum2 = 2;
    private final int mSum3 = 3;
    private final int mSum4 = 4;
    private final int mSum5 = 5;
    private final int mSum6 = 6;
    private final int mSum7 = 7;
    private final int mSum8 = 8;
    private final int mSum9 = 9;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        try {
            stringdata = getResourceManager().getElement(ResourceTable.Strarray_tab_title).getStringArray();

        } catch (IOException e) {
            HiLog.info(LABEL_LOG, "IOException");
        } catch (NotExistException e) {
            HiLog.info(LABEL_LOG, "NotExistException");
        } catch (WrongTypeException e) {
            HiLog.info(LABEL_LOG, "WrongTypeException");
        }
        initview();
    }

    private void initview() {
        mainlistcontainer = (ListContainer) findComponentById(ResourceTable.Id_mainlistcontainer);
        SampleItemProvider sampleItemProvider = new SampleItemProvider(getdata(), this);
        mainlistcontainer.setItemProvider(sampleItemProvider);
        mainlistcontainer.setItemClickedListener((listContainer, component, post, l1) -> {
           if (post == mSum1 || post == mSum2 || post == mSum4) {
                Intent secondIntent = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName(this.getBundleName())
                        .withAbilityName(ViewPageAbility.class.getName())
                        .build();
                secondIntent.setOperation(operation);
                startAbility(secondIntent);
            } else if (post == mSum3) {
                Intent secondIntent = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName(this.getBundleName())
                        .withAbilityName(ColorViewpageAbility.class.getName())
                        .build();
                secondIntent.setOperation(operation);
                startAbility(secondIntent);
            } else if (post == mSum6 || post == mSum7) {
                Intent secondIntent = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName(this.getBundleName())
                        .withAbilityName(TwoViewpageability.class.getName())
                        .build();
                secondIntent.setOperation(operation);
                startAbility(secondIntent);
            } else {
                Intent secondIntent = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName(this.getBundleName())
                        .withAbilityName(ListContainerAbility.class.getName())
                        .build();
                secondIntent.setOperation(operation);
                startAbility(secondIntent);
            }
        });
    }

    /**
     * getdata
     *
     * @return list
     */
    private ArrayList getdata() {
        ArrayList<SampleItem> arrayList = new ArrayList<>();

        for (int i = 0; i < mSum9; i++) {
            SampleItem sampleItem = new SampleItem();
            if (i == 0) {
                sampleItem.setName(stringdata[0]);
                sampleItem.setPost(1);
            } else if (i == mSum5) {
                sampleItem.setName(stringdata[1]);
                sampleItem.setPost(1);
            } else if (i == mSum1) {
                sampleItem.setName("normal coverflow");
                sampleItem.setConnet("a simple demo");
                sampleItem.setPost(0);
            } else if (i == mSum2) {
                sampleItem.setName("transformer coverflow");
                sampleItem.setConnet("a simple demo with page transformer");
                sampleItem.setPost(0);
            } else if (i == mSum3) {
                sampleItem.setName("transformer coverflow 2");
                sampleItem.setConnet("a simple demo with page transformer");
                sampleItem.setPost(0);
            } else if (i == mSum4) {
                sampleItem.setName("Overlapping CoverFlow");
                sampleItem.setConnet("a simple demo with overlapping viewpager");
                sampleItem.setPost(0);
            } else if (i == mSum6) {
                sampleItem.setName("linkage coverflow");
                sampleItem.setConnet(connect);
                sampleItem.setPost(0);
            } else if (i == mSum7) {
                sampleItem.setName("linkage1 coverflow");
                sampleItem.setConnet(connect);
                sampleItem.setPost(0);
            } else {
                sampleItem.setName("linkage2 coverflow");
                sampleItem.setConnet(connect);
                sampleItem.setPost(0);
            }
            arrayList.add(sampleItem);
        }
        return arrayList;
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
